﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneralLibs.Lists;
using System.Collections;
using Security;

namespace Portal.DanhMuc
{
    public partial class FrmDanhMucCho : FrmBaseListForm
    {
        public FrmDanhMucCho()
        {
            InitializeComponent();
        }

        private void FrmDanhMucCho_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            gridDSDoiTuong.Visible = false;
            tlvDanhMucCho.OptionsBehavior.Editable = false;
            //tlvDanhMucCho.ParentFieldName = "MaChoChaID";
            tlvDanhMucCho.ExpandAll();
        }
        void Instance_Change()
        {
            LoadData();
        }
        public override void LoadData()
        {
            try
            {
                tlvDanhMucCho.DataSource = DanhMucCho.Instance.DataView;
                InitGridColumnNews();
            }
            catch(Exception ex)
            {
            }
        }
        public void InitGridColumnNews()
        {
            foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in tlvDanhMucCho.Columns)
            {
                column.Visible = false;
            }

            //tlvDanhMucCho.Columns["MaCho"].Caption = "Mã chợ";
            //tlvDanhMucCho.Columns["MaCho"].Visible = true;
            //tlvDanhMucCho.Columns["MaCho"].VisibleIndex = 1;
            tlvDanhMucCho.Columns["TenCho"].Caption = "Tên chợ";
            tlvDanhMucCho.Columns["TenCho"].Visible = true;
            tlvDanhMucCho.Columns["TenCho"].VisibleIndex = 2;
            tlvDanhMucCho.Columns["HangCho"].Caption = "Hạng chợ";
            tlvDanhMucCho.Columns["HangCho"].Visible = true;
            tlvDanhMucCho.Columns["HangCho"].VisibleIndex = 3;
            tlvDanhMucCho.Columns["DiaBanID"].Caption = "Địa bàn";
            tlvDanhMucCho.Columns["DiaBanID"].Visible = true;
            tlvDanhMucCho.Columns["DiaBanID"].VisibleIndex = 3;

            //lookup cho DiaBanID
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repLookupDiaBan = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            repLookupDiaBan.ValueMember = "ID";
            repLookupDiaBan.DisplayMember = "TenDiaBan";
            repLookupDiaBan.DataSource = GeneralLibs.Lists.DanhMucDiaBan.Instance.DataTable;
            repLookupDiaBan.NullText = "";

            tlvDanhMucCho.Columns["DiaBanID"].ColumnEdit = repLookupDiaBan;
        }

        private void TlvDanhMucCho_DoubleClick(object sender, EventArgs e)
        {
            Edit();        
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditCho frm = new FrmEditCho(null);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    DanhMucCho.Instance.LoadFromDB();
                }

            }
            catch(Exception ex)
            {
            }
        }
        protected override void Edit()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucCho.Selection[0]["ID"];
                FrmEditCho frm = new FrmEditCho(DanhMucCho.Instance.GetByID(id));
                frm.ShowDialog();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        protected override void Delete()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucCho.Selection[0]["ID"];
                Cho deletedObj = DanhMucCho.Instance.GetByProperty("ID", id);
                if (deletedObj != null)
                {
                    if (MessageBox.Show("Bạn có thực sự muốn xóa danh mục chợ?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                    else
                        deletedObj.DeleteFromDB();
                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
